package my.OrderMessage;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class C2DMReceiver extends BroadcastReceiver {
	public void onReceive(Context context, Intent intent) {
    	if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
    		handleRegistration(context, intent);
    	} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
    		String c2dm_msg = intent.getExtras().getString("msg");
    		Toast toast = Toast.makeText(context, "메시지 도착!\n"+c2dm_msg, Toast.LENGTH_SHORT );
 			toast.setGravity( Gravity.TOP | Gravity.CENTER, 0, 150 );
 			toast.show();
// 			Toast.makeText(context, c2dm_msg, 1000).show();
    	}
    }
    
    private void handleRegistration(Context context, Intent intent) {
    	String registration_id = intent.getStringExtra("registration_id");
    		
    	System.out.println("registration_id====>"+registration_id);
    	
    	if (intent.getStringExtra("error") != null) {
    		Log.e("C2DM_REGISTRATION",">>>>>" + "Registration failed, should try again later." + "<<<<<");
    	
    	} else if (intent.getStringExtra("unregistered") != null) {
    		Log.e("C2DM_REGISTRATION",">>>>>" + "unregistration done, new messages from the authorized sender will be rejected" + "<<<<<");
    	
    	} else if (registration_id != null) {
    		// 핸폰번호와 핸폰고유 아이디가 없으면 DB INSERT
    		DbGetData dbGetData = new DbGetData();
    		List<NameValuePair> params = new ArrayList<NameValuePair>();
    		String name = null;
    		
    		// 다른 액티비티의 레이아웃 값(이름)을 가져옴 
    		if ( PhoneRegistration.mContext != null ) {
    			name = ((PhoneRegistration)PhoneRegistration.mContext).returnName();
    		}
    		UserMethod userMethod = new UserMethod();
            String phoneNumber =  userMethod.getMyPhoneNumber(context);

    		params.add(new BasicNameValuePair("name", name));
    		params.add(new BasicNameValuePair("hpNum", phoneNumber));
    		params.add(new BasicNameValuePair("rId", registration_id));
    		String result = dbGetData.dbGetData("http://118.32.182.248:8080/JSON_JSP_SERVER/OrderMessage/R_ID_INSERT_01.jsp", params);
    		
    		try {
    			JSONObject json = new JSONObject(result);
    			result = json.getString("result");
    		} catch (Exception e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		    		
    		if ( result.equals("Y")) {
    	  		((PhoneRegistration)PhoneRegistration.mContext).mProgressDialog.dismiss();
    			((PhoneRegistration)PhoneRegistration.mContext).startMain();
    		} else {
    			Toast.makeText(context, "푸시서버에 제대로 등록이 되지 않았습니다.", Toast.LENGTH_SHORT).show();
    		}
    		
//    		System.out.println("인서트확인" + result); 
//    		System.out.println("registration_id complete!!");
    	}
    }
    
    private String getMyPhoneNumber(Context context){
    	TelephonyManager mTelephonyMgr;  
    	mTelephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);   
    	return mTelephonyMgr.getLine1Number();
    }
}
